package com.example.blue.imagesearch;

import android.content.Context;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.os.ParcelFormatException;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ProgressBar;
import android.widget.Toast;

import util.ImageLoader;
import util.Search;

/**
 * Created by blue on 2016/7/1.
 */
public class ListImgsFragment extends Fragment implements View.OnClickListener {
    private GridView gridView;
    private Button btn_startSearch;
    private EditText edit_infor;
    private String[] urlArray;
    private ImageLoader mImageLoader;
    private Handler UIHandler = new Handler();

    private ProgressBar progressBar;

    private Runnable stopRunable;
    private Runnable runnable;


    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        mImageLoader = ImageLoader.getInstance(3, ImageLoader.Type.LIFO);
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_list_imgs, container, false);
        gridView = (GridView) view.findViewById(R.id.id_gridview);
        btn_startSearch = (Button) view.findViewById(R.id.btn_startSearch);
        edit_infor = (EditText) view.findViewById(R.id.edit_infor);
        progressBar = (ProgressBar) view.findViewById(R.id.progress_bar);
        btn_startSearch.setOnClickListener(this);
        UIHandler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                if (msg.what == 1) {
                    Log.e("ListLmgFragment", "开启进度条");
                    progressBar.setVisibility(View.VISIBLE);
                }
                if (msg.what == 2) {
                    Log.e("ListLmgFragment", "关闭进度条");
                    progressBar.setVisibility(View.GONE);
                    setAdapter();
                }
            }
        };
        return view;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_startSearch:

                progressBar.setVisibility(View.VISIBLE);
                new Thread() {
                    @Override
                    public void run() {
                        Search search = new Search(edit_infor.getText().toString(), UIHandler);
                        urlArray = search.GetUrl().toString().split("\n");
                        System.out.println("图片个数:" + urlArray.length);
                        UIHandler.sendEmptyMessage(2);

                    }
                }.start();
                break;
        }
    }

    private void setAdapter() {
        if (getActivity() == null || gridView == null)
            return;
        if (urlArray != null) {


            gridView.setAdapter(new ListImgItemAdapetr(getActivity(), 0, urlArray));


        }
    }

    private class ListImgItemAdapetr extends ArrayAdapter<String> {
        public ListImgItemAdapetr(Context context, int resouce, String[] datas) {
            super(getActivity(), 0, datas);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                convertView = getActivity().getLayoutInflater().inflate(R.layout.item_fragment_list_imgs, parent, false);
            }
            ImageView imageView = (ImageView) convertView.findViewById(R.id.id_img);
            imageView.setImageResource(R.drawable.pictures_no);
            mImageLoader.loadImage(getItem(position), imageView, true);
            return convertView;

        }
    }
}
